﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WPF_QCCM;
using WPF_QCCM.ServiceReference1;

namespace WPF_QCCM
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>

    public enum AttribMeasurementTypes
    {
        [Description("Target/Tolerance")]
        TargetTolerance,
        [Description("High/Low Limits")]
        HighLow
    }

    public partial class MainWindow : Window
    {

        List<Material> Materials = new List<Material>();
        List<QCAttrib> QCAttribs = new List<QCAttrib>();
        List<MaterialQCAttrib> MQCAttribs = new List<MaterialQCAttrib>();
        List<QCSchedule> QCSchedules = new List<QCSchedule>();


        public MainWindow()
        {
            InitializeComponent();
            WindowStartupLocation = System.Windows.WindowStartupLocation.CenterScreen;
        }

        #region Material functions

        private void dgdMaterials_Initialized(object sender, EventArgs e)
        {
            ServiceReference1.QCAServiceClient sr1 = new ServiceReference1.QCAServiceClient();
            Material[] materials = sr1.GetAllMaterials();
            Materials = new List<Material>(materials);
            dgdMaterials.ItemsSource = Materials;

        }

        private void dgdMaterials_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            btnAddMaterial.Visibility = System.Windows.Visibility.Collapsed;
            btnSaveMaterial.Visibility = System.Windows.Visibility.Visible;
            btnDeleteMaterial.Visibility = System.Windows.Visibility.Visible;
            btnMcancel.Visibility = System.Windows.Visibility.Visible;

            Material qcm = ((Material)dgdMaterials.SelectedItem);
            if (dgdMaterials.SelectedIndex != -1)
            {
                txtMname.Text = qcm.MaterialName;
                txtMdescription.Text = qcm.MaterialDescription;
                chkMdisabled.IsChecked = qcm.MaterialDisabled;
            }
        }

        private void btnAddMaterial_Click(object sender, RoutedEventArgs e)
        {
            string msg = "";
            ServiceReference1.QCAServiceClient sr1 = new ServiceReference1.QCAServiceClient();
            msg = sr1.AddMaterial(txtMname.Text, txtMdescription.Text, chkMdisabled.IsChecked.Value);
            MessageBox.Show(msg);

            ClearMaterialTextBoxes();
            HideMaterialButtons();
            dgdMaterials_Initialized(sender, e);
        }

        private void btnSaveMaterial_Click(object sender, RoutedEventArgs e)
        {
            string msg = "";
            ServiceReference1.QCAServiceClient sr1 = new ServiceReference1.QCAServiceClient();
            msg = sr1.UpdateMaterial(txtMname.Text, txtMdescription.Text, chkMdisabled.IsChecked.Value);
            MessageBox.Show(msg);

            ClearMaterialTextBoxes();
            dgdMaterials_Initialized(sender, e);
            HideMaterialButtons();
        }

        private void btnDeleteMaterial_Click(object sender, RoutedEventArgs e)
        {
            string msg = "";
            ServiceReference1.QCAServiceClient sr1 = new ServiceReference1.QCAServiceClient();
            msg = sr1.DeleteMaterial(txtMname.Text);
            MessageBox.Show(msg);

            ClearMaterialTextBoxes();
            dgdMaterials_Initialized(sender, e);
            HideMaterialButtons();
        }

        private void btnMcancel_Click(object sender, RoutedEventArgs e)
        {
            ClearMaterialTextBoxes();
            dgdMaterials.SelectedIndex = -1;
            dgdMaterials_Initialized(sender, e);
            HideMaterialButtons();
        }

        private void btnMsearch_Click(object sender, RoutedEventArgs e)
        {
            foreach (Material m in dgdMaterials.ItemsSource)
            {
                if (m.MaterialName == txtMsearch.Text)
                {
                    dgdMaterials.SelectedItem = m;
                }
            }
            txtMsearch.Clear();
        }
        private void txtMsearch_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                btnMsearch_Click(this, new RoutedEventArgs());
            }
        }

        private void ClearMaterialTextBoxes()
        {
            txtMname.Text = "";
            txtMdescription.Text = "";
            chkMdisabled.IsChecked = false;

            //dgdMaterials.UnselectAll();
        }
        private void HideMaterialButtons()
        {
            btnAddMaterial.Visibility = System.Windows.Visibility.Visible;
            btnSaveMaterial.Visibility = System.Windows.Visibility.Collapsed;
            btnDeleteMaterial.Visibility = System.Windows.Visibility.Collapsed;
            btnMcancel.Visibility = System.Windows.Visibility.Collapsed;
        }

        private void ShowMaterialButtons()
        {
            btnAddMaterial.Visibility = System.Windows.Visibility.Collapsed;
            btnSaveMaterial.Visibility = System.Windows.Visibility.Visible;
            btnDeleteMaterial.Visibility = System.Windows.Visibility.Visible;
            btnMcancel.Visibility = System.Windows.Visibility.Visible;
        }


        #endregion Material functions


        #region Attribute functions

        private void dgdAttributes_Initialized(object sender, EventArgs e)
        {
            ServiceReference1.QCAServiceClient sr1 = new ServiceReference1.QCAServiceClient();
            QCAttrib[] attributes = sr1.GetAttribs();
            QCAttribs = new List<QCAttrib>(attributes);
            dgdAttributes.ItemsSource = QCAttribs;
        }

        private void dgdAttributes_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            QCAttrib qca = ((QCAttrib)dgdAttributes.SelectedItem);
            if (dgdAttributes.SelectedItem != null)
            {
                btnAddQCAttribute.Visibility = System.Windows.Visibility.Collapsed;
                btnSaveAttribute.Visibility = System.Windows.Visibility.Visible;
                btnDeleteAttribute.Visibility = System.Windows.Visibility.Visible;
                btnAcancel.Visibility = System.Windows.Visibility.Visible;

                txtAname.Text = qca.Name;
                txtAdescription.Text = qca.Desc;
                txtDataType.Text = qca.TypeOfData;
                chkAdisabled.IsChecked = qca.Disabled;
            }

        }

        private void btnAddAttribute_Click(object sender, RoutedEventArgs e)
        {
            string msg = "";
            ServiceReference1.QCAServiceClient sr1 = new ServiceReference1.QCAServiceClient();
            msg = sr1.AddQCAttrib(txtAname.Text, txtDataType.Text, txtAdescription.Text, chkAdisabled.IsChecked.Value);
            MessageBox.Show(msg);

            HideAttributeButtons();
            ClearAttributeTextBoxes();
            dgdAttributes_Initialized(sender, e); 

            //MessageBox.Show("I'm not working yet!\rTry again soon.");
        }

        private void btnDeleteAttribute_Click(object sender, RoutedEventArgs e)
        {
            string msg = "";
            ServiceReference1.QCAServiceClient sr1 = new ServiceReference1.QCAServiceClient();
            msg = sr1.DeleteQCAttrib(txtAname.Text);
            MessageBox.Show(msg);

            HideAttributeButtons();
            ClearAttributeTextBoxes();
            dgdAttributes_Initialized(sender, e);
            
            //MessageBox.Show("I'm not working yet!\rTry again soon.");
        }

        private void btnSaveAttribute_Click(object sender, RoutedEventArgs e)
        {
            string msg = "";
            ServiceReference1.QCAServiceClient sr1 = new ServiceReference1.QCAServiceClient();
            msg = sr1.UpdateQCAttrib(txtAname.Text, txtDataType.Text, txtAdescription.Text, chkAdisabled.IsChecked.Value);
            MessageBox.Show(msg);

            HideAttributeButtons();
            ClearAttributeTextBoxes();
            dgdAttributes_Initialized(sender, e);

        }

        private void btnAsearch_Click(object sender, RoutedEventArgs e)
        {
            foreach (QCAttrib v in dgdAttributes.ItemsSource)
            {
                if(v.Name == txtAsearch.Text)
                {
                    dgdAttributes.SelectedItem = v;
                }
            }
            txtAsearch.Clear();   
        }
        private void txtAsearch_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                btnAsearch_Click(this, new RoutedEventArgs());
            }
        }

        private void btnAcancel_Click(object sender, RoutedEventArgs e)
        {
            HideAttributeButtons();
            ClearAttributeTextBoxes();
            dgdAttributes_Initialized(sender, e);
        }

        private void ClearAttributeTextBoxes()
        {
            txtAname.Text = "";
            txtAdescription.Text = "";
            txtDataType.Text = "";
            chkAdisabled.IsChecked = false;

            //dgdAttributes.UnselectAll();
        }
        private void HideAttributeButtons()
        {
            btnAddQCAttribute.Visibility = System.Windows.Visibility.Visible;
            btnSaveAttribute.Visibility = System.Windows.Visibility.Collapsed;
            btnDeleteAttribute.Visibility = System.Windows.Visibility.Collapsed;
            btnAcancel.Visibility = System.Windows.Visibility.Collapsed;
        }

        private void ShowAttributeButtons()
        {
            btnAddQCAttribute.Visibility = System.Windows.Visibility.Collapsed;
            btnSaveAttribute.Visibility = System.Windows.Visibility.Visible;
            btnDeleteAttribute.Visibility = System.Windows.Visibility.Visible;
            btnAcancel.Visibility = System.Windows.Visibility.Visible;
        }

        #endregion


        #region Material QC Attributes

        private void lstMaterials_2_Initialized(object sender, EventArgs e)
        {
            lstMaterials_2.ItemsSource = Materials;
            lstMaterials_2.DisplayMemberPath = "MaterialName";
        }

        private void lstAttributes_2_Initialized(object sender, EventArgs e)
        {
            lstAttributes_2.ItemsSource = QCAttribs;
            lstAttributes_2.DisplayMemberPath = "Name";
        }

        private void dgdMQCAttribs_Initialized(object sender, EventArgs e)
        {
            ServiceReference1.QCAServiceClient sr1 = new ServiceReference1.QCAServiceClient();
            MaterialQCAttrib[] mQCAttribs = sr1.GetAllMaterialQCAttrib();
            MQCAttribs = new List<MaterialQCAttrib>(mQCAttribs);
            dgdMQCAttribs.ItemsSource = MQCAttribs;
        }

        private void btnMsearch_2_Click(object sender, RoutedEventArgs e)
        {
            {
                foreach (Material m in lstMaterials_2.ItemsSource)
                {
                    if (m.MaterialName == txtMsearch.Text)
                    {
                        lstMaterials_2.SelectedItem = m;
                    }
                }
                //foreach (MaterialQCAttrib mqca in dgdMQCAttribs.ItemsSource)
                //{
                    
                //}

                txtMsearch.Clear();
            }
        }

        private void btnMsearch_2_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                btnMsearch_2_Click(this, new RoutedEventArgs());
            }
        }


        #endregion

        private void dgdMQCAttribs_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            MaterialQCAttrib mqca = ((MaterialQCAttrib)dgdMQCAttribs.SelectedItem);
            ShowMQCAeditButtons();
            if (dgdMQCAttribs.SelectedIndex != -1)
            {
                Material m = new Material();
                QCAttrib a = new QCAttrib();
                m.MaterialID = mqca.MaterialID;
                a.ID = mqca.QCAttribID;

                foreach (Material m2 in lstMaterials_2.ItemsSource)
                {
                    if (m2.MaterialID == m.MaterialID)
                    {
                        lstMaterials_2.SelectedItem = m2;
                        txtMsearch_2.Text = m2.MaterialName;
                    }
                }
                foreach (QCAttrib a2 in lstAttributes_2.ItemsSource)
                {
                    if (a2.ID == a.ID)
                    {
                        lstAttributes_2.SelectedItem = a2;
                        txtAsearch_2.Text = a2.Name;
                    }
                }
                //string var = Convert.ToString(mqca.MeasurementType);
                //cboType.SelectedValue = var;
                if (mqca.MeasurementType == "TargetThreshold")
                {
                    cboType.SelectedIndex = 0;
                }
                else if (mqca.MeasurementType == "UpperLower")
                {
                    cboType.SelectedIndex = 1;
                }

                txtTarget.Text = Convert.ToString(mqca.Target);
                txtTolerance.Text = Convert.ToString(mqca.Threshold);
                txtHigh.Text = Convert.ToString(mqca.Upper);
                txtLow.Text = Convert.ToString(mqca.Lower);
                chkMQCAdisabled.IsChecked = mqca.MaterialQCADisabled;
            }

        }

        private void cboType_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (cboType.SelectedIndex == 0)
            {
                TargetVisible();
                txtHigh.Text = "0";
                txtLow.Text = "0";
            }
            else if (cboType.SelectedIndex == 1)
            {
                HighLowVisible();
                txtTarget.Text = "0";
                txtTolerance.Text = "0";
            }
        }
        private void TargetVisible()
        {
            Target.Visibility = System.Windows.Visibility.Visible;
            Tolerance.Visibility = System.Windows.Visibility.Visible;
            Percent.Visibility = System.Windows.Visibility.Visible;
            txtTarget.Visibility = System.Windows.Visibility.Visible;
            txtTolerance.Visibility = System.Windows.Visibility.Visible;

            High.Visibility = System.Windows.Visibility.Hidden;
            Low.Visibility = System.Windows.Visibility.Hidden;
            txtHigh.Visibility = System.Windows.Visibility.Hidden;
            txtLow.Visibility = System.Windows.Visibility.Hidden;
        }
        private void HighLowVisible()
        {
            Target.Visibility = System.Windows.Visibility.Hidden;
            Tolerance.Visibility = System.Windows.Visibility.Hidden;
            Percent.Visibility = System.Windows.Visibility.Hidden;
            txtTarget.Visibility = System.Windows.Visibility.Hidden;
            txtTolerance.Visibility = System.Windows.Visibility.Hidden;

            High.Visibility = System.Windows.Visibility.Visible;
            Low.Visibility = System.Windows.Visibility.Visible;
            txtHigh.Visibility = System.Windows.Visibility.Visible;
            txtLow.Visibility = System.Windows.Visibility.Visible;
        }
        private void HideMQCAtextBoxes()
        {
            Target.Visibility = System.Windows.Visibility.Hidden;
            Tolerance.Visibility = System.Windows.Visibility.Hidden;
            Percent.Visibility = System.Windows.Visibility.Hidden;
            txtTarget.Visibility = System.Windows.Visibility.Hidden;
            txtTolerance.Visibility = System.Windows.Visibility.Hidden;

            High.Visibility = System.Windows.Visibility.Hidden;
            Low.Visibility = System.Windows.Visibility.Hidden;
            txtHigh.Visibility = System.Windows.Visibility.Hidden;
            txtLow.Visibility = System.Windows.Visibility.Hidden;
        }


        private void ShowMQCAeditButtons()
        {
            btnAddMQCA.Visibility = System.Windows.Visibility.Hidden;
            btnSaveMQCA.Visibility = System.Windows.Visibility.Visible;
            btnDeleteMQCA.Visibility = System.Windows.Visibility.Visible;
            btnCancelMQCA.Visibility = System.Windows.Visibility.Visible;
        }

        private void HideMQCAeditButtons()
        {
            btnAddMQCA.Visibility = System.Windows.Visibility.Visible;
            btnSaveMQCA.Visibility = System.Windows.Visibility.Hidden;
            btnDeleteMQCA.Visibility = System.Windows.Visibility.Hidden;
            btnCancelMQCA.Visibility = System.Windows.Visibility.Hidden;
        }

        private void cboType_Initialized(object sender, EventArgs e)
        {
            //cboType.ItemsSource = AttribMeasurementTypes;
            //cboType.DisplayMemberPath = "Description";
            //cboType.SelectedValue = "value";
        }

        private void lstMaterials_2_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            Material qcm = ((Material)lstMaterials_2.SelectedItem);
            if (lstMaterials_2.SelectedIndex != -1)
            {
                txtMsearch_2.Text = qcm.MaterialName;
            }

        }

        private void lstAttributes_2_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            QCAttrib qca = ((QCAttrib)lstAttributes_2.SelectedItem);
            if (lstAttributes_2.SelectedItem != null)
            {
                txtAsearch_2.Text = qca.Name;
            }
        }

        private void btnAddMQCA_Click(object sender, RoutedEventArgs e)
        {
            MaterialQCAttrib mqca = new MaterialQCAttrib();
            Material m = ((Material)lstMaterials_2.SelectedItem);
            QCAttrib a = ((QCAttrib)lstAttributes_2.SelectedItem);
            bool b = false;

            ServiceReference1.QCAServiceClient sr1 = new ServiceReference1.QCAServiceClient();

            mqca.MaterialID = m.MaterialID;
            mqca.QCAttribID = a.ID;
            mqca.MeasurementType = Convert.ToString(cboType.SelectedItem);
            mqca.Target = Convert.ToDecimal(txtTarget.Text);
            mqca.Threshold = Convert.ToDecimal(txtTolerance.Text);
            mqca.Upper = Convert.ToDecimal(txtHigh.Text);
            mqca.Lower = Convert.ToDecimal(txtLow.Text);
            mqca.MaterialQCADisabled = chkMQCAdisabled.IsChecked.Value;

            b = sr1.AddMaterialQCA(mqca);

            if (b == false)
            {
                MessageBox.Show("'Houston, we have a problem'\rMaterial QC Attribute not added");
            }
            else if (b == true)
            {
                MessageBox.Show("Added successfully");
            }
            else
            {
                MessageBox.Show("Wait...what just happened?\rSomething didn't work.");
            }

            dgdMQCAttribs_Initialized(sender, e);
        }

        private void btnCancelMQCA_Click(object sender, RoutedEventArgs e)
        {
            HideMQCAeditButtons();
            dgdMQCAttribs.SelectedItem = -1;
            lstMaterials_2.UnselectAll();
            txtMsearch_2.Clear();
            txtAsearch_2.Clear();
            lstAttributes_2.UnselectAll();
            cboType.SelectedItem = -1;
            HideMQCAtextBoxes();
            chkMQCAdisabled.IsChecked = false;
        }

        private void btnDeleteMQCA_Click(object sender, RoutedEventArgs e)
        {
            MaterialQCAttrib mqca = new MaterialQCAttrib();
            mqca = ((MaterialQCAttrib)dgdMQCAttribs.SelectedItem);
            bool b = false;
            ServiceReference1.QCAServiceClient sr1 = new ServiceReference1.QCAServiceClient();

            b = sr1.DeleteMaterialQCAttrib(mqca);

            if (b == false)
            {
                MessageBox.Show("'Houston, we have a problem'\rMaterial QC Attribute not deleted");
            }
            else if (b == true)
            {
                MessageBox.Show("Deleted successfully");
            }
            else
            {
                MessageBox.Show("Wait...what just happened?\rSomething didn't work.");
            }

            dgdMQCAttribs_Initialized(sender, e);

        }

        private void btnSaveMQCA_Click(object sender, RoutedEventArgs e)
        {
            MaterialQCAttrib mqca = new MaterialQCAttrib();
            Material m = ((Material)lstMaterials_2.SelectedItem);
            QCAttrib a = ((QCAttrib)lstAttributes_2.SelectedItem);
            bool b = false;

            ServiceReference1.QCAServiceClient sr1 = new ServiceReference1.QCAServiceClient();

            mqca.MaterialID = m.MaterialID;
            mqca.QCAttribID = a.ID;
            mqca.MeasurementType = Convert.ToString(cboType.SelectedItem);
            mqca.Target = Convert.ToDecimal(txtTarget.Text);
            mqca.Threshold = Convert.ToDecimal(txtTolerance.Text);
            mqca.Upper = Convert.ToDecimal(txtHigh.Text);
            mqca.Lower = Convert.ToDecimal(txtLow.Text);
            mqca.MaterialQCADisabled = chkMQCAdisabled.IsChecked.Value;

            b = sr1.UpdateMaterialQCA(mqca);

            if (b == false)
            {
                MessageBox.Show("'Houston, we have a problem'\rMaterial QC Attribute not updated");
            }
            else if (b == true)
            {
                MessageBox.Show("Updated successfully");
            }
            else
            {
                MessageBox.Show("Wait...what just happened?\rSomething didn't work.");
            }

            dgdMQCAttribs_Initialized(sender, e);

        }

        private void dgdSchedules_Initialized(object sender, EventArgs e)
        {
            ServiceReference1.QCAServiceClient sr1 = new ServiceReference1.QCAServiceClient();
            QCSchedule[] schedules = sr1.GetAllQCSchedules();
            QCSchedules = new List<QCSchedule>(schedules);
            dgdSchedules.ItemsSource = QCSchedules;
        } 


    }
}
